---
title: 'Enrich with OpenTelemetry API'
sidebarTitle: 'Enrichment'
icon: 'telescope'
---

import EnrichmentIntro from '/snippets/enrichment-intro.mdx'

<EnrichmentIntro language='PHP' />

## Required Dependencies

Add the following dependencies to your project by running:

```bash
composer require open-telemetry/api
```

## Creating a Span

To create a new span, use the `TracerProvider`.

```php
<?php

use OpenTelemetry\API\Globals;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/vendor/autoload.php';

$tracer = Globals::tracerProvider()->getTracer('demo');

$app = AppFactory::create();
$app->get('/rolldice', function (Request $request, Response $response) use ($tracer) {
    $span = $tracer
        ->spanBuilder('manual-span')
        ->startSpan();

    $result = random_int(1,6);
    $response->getBody()->write(strval($result));

    $span
        ->addEvent('rolled dice', ['result' => $result])
        ->end();
    return $response;
});

$app->run();
```

## Additional Information

For more use cases, see the [OpenTelemetry PHP API documentation](https://opentelemetry.io/docs/languages/php).
